package com.tlibrary.service;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tlibrary.dao.*;
import com.tlibrary.model.MultiplechoiceWithBLOBs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Author：wcc
 * @Description：
 * @Date：Create in: 2019/2/15--17:14
 */
@Service
public class TeacherService {

    @Autowired
    private MultiplechoiceMapper mm;
    @Autowired
    private CollegeMapper cm;
    @Autowired
    private LabelsMapper lm;
    @Autowired
    private SubquestionsMapper sm;
    @Autowired
    private AnsquestionsMapper am;
    @Autowired
    private UserMapper um;

    @Transactional
    public void inputmul(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int count = 0;
        String[] t1 = request.getParameterValues("t1");
        String[] a = request.getParameterValues("a");
        String[] b = request.getParameterValues("b");
        String[] c = request.getParameterValues("c");
        String[] d = request.getParameterValues("d");
        String[] e = request.getParameterValues("e");
        String[] f = request.getParameterValues("f");
        String[] nd = request.getParameterValues("nddj");
        String userid = "f9876a64-255c-4f05-837a-8a837477ec33";
        // String userid=request.getSession().getAttribute("id").toString();
        for (int i = 0; i < t1.length; i++) {
            MultiplechoiceWithBLOBs mulselect = new MultiplechoiceWithBLOBs();
            mulselect.setTime(new Date());
            mulselect.setTitle(t1[i]);
            mulselect.setId(UUID.randomUUID().toString());
            mulselect.setUser(userid);

            if (!a[i].equals("")) {
                mulselect.setA(a[i]);
            }
            if (!b[i].equals("")) {
                mulselect.setB(b[i]);
            }
            if (!c[i].equals("")) {
                mulselect.setC(c[i]);
            }
            if (!d[i].equals("")) {
                mulselect.setD(d[i]);
            }
            if (!e[i].equals("")) {
                mulselect.setE(e[i]);
            }
            if (!f[i].equals("")) {
                mulselect.setF(f[i]);
            }

            mm.insertSelective(mulselect);
            //添加成功时将难度等级加入到标签表
            if (!nd.equals("")) {
                HashMap hashMap = new HashMap();
                hashMap.put("nd", nd[i]);
                hashMap.put("ID", mulselect.getId());
                lm.insertlabel(hashMap);
            }
        }

    }

    public HashMap findmul(int pageNum,int pageSize,HashMap map) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, String>> muls = mm.getAllMul(map);
        PageInfo<Map<String, String>> pageInfo = new PageInfo<Map<String, String>>(muls);
        HashMap returnMap = new HashMap();
        returnMap.put("total", pageInfo.getTotal());
        returnMap.put("rows", pageInfo.getList());
        return returnMap;
    }


    public HashMap findCouser(HttpServletRequest request, HttpServletResponse response) {
        //        HttpSession session=request.getSession();

//      String userid = session.getAttribute("id").toString();

        Integer pageNum = Integer.valueOf(request.getParameter("page"));
        String userid = "f9876a64-255c-4f05-837a-8a837477ec33";
        Integer pageSize = Integer.valueOf(request.getParameter("rows"));
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, String>> muls = cm.getCouser(userid);
        PageInfo<Map<String, String>> pageInfo = new PageInfo<Map<String, String>>(muls);
        HashMap returnMap = new HashMap();
        returnMap.put("total", pageInfo.getTotal());
        returnMap.put("rows", pageInfo.getList());
        return returnMap;
    }

    public void addcouser(HashMap map) throws Exception {
        cm.addCouser(map);
    }

    public void editcouser(HashMap map) throws Exception {
        cm.editCouser(map);
    }

    public void delUser(JSONArray array) throws Exception {
        for (int i = 0; i < array.size(); i++) {
            Map<String, String> o = (Map) array.get(i);
            String id = o.get("ID");
            cm.delCouser(id);
        }
    }

    public void updatemulAndLab(HashMap hashMap) throws Exception {
        MultiplechoiceWithBLOBs multiplechoiceWithBLOBs = new MultiplechoiceWithBLOBs();
       // ArrayList ans = (hashMap.get("ans").toString());
//        if (!ans.isEmpty()){
//            String ansstring=null;
//            for (Iterator iterator = ans.iterator(); iterator.hasNext(); ) {
//                ansstring +=  iterator.next()+",";
//            }
 //          hashMap.put("ans",ansstring);
//        }
        mm.updatebykey(hashMap);
        String count = lm.selectCountByKey(hashMap.get("ID").toString());
        if (count.equals("0")) {
            //为0就新增
            lm.insertlabel(hashMap);
        } else {
            //否则就更新
            lm.updateLabel(hashMap);
        }
    }

    public void addsubquestion(HashMap hashMap) {

        hashMap.put("id",UUID.randomUUID().toString());
        //        String userid = session.getAttribute("id").toString();
//               HttpSession session=request.getSession();
        String userid = "f9876a64-255c-4f05-837a-8a837477ec33";
        hashMap.put("userid",userid);
        hashMap.put("content",hashMap.get("tf").toString().replace("&nbsp;",""));
        hashMap.put("time",new Date());
        //判断题目类型，填空和判断为一个类型
        //简答题为一个类型
        if (null!=hashMap.get("type")&&!"简答题".equals(hashMap.get("type"))){
            sm.insertFillin(hashMap);
        }
        if (null!=hashMap.get("type")&&"简答题".equals(hashMap.get("type"))){
            am.insertsub(hashMap);
        }

    }

    public HashMap findFAndTF(HashMap hashMap, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, String>> muls = sm.getAllFandTF(hashMap);
        PageInfo<Map<String, String>> pageInfo = new PageInfo<Map<String, String>>(muls);
        HashMap returnMap = new HashMap();
        returnMap.put("total", pageInfo.getTotal());
        returnMap.put("rows", pageInfo.getList());
        return returnMap;
    }

    public void delMul(JSONArray array) throws  Exception {
        for (int i = 0; i <array.size() ; i++) {
            String id = ((Map)array.get(i)).get("ID").toString();
            mm.deleteByPrimaryKey(id);
        }
    }

    public void delfandtf(JSONArray array) {
        for (int i = 0; i <array.size() ; i++) {
            String id = ((Map)array.get(i)).get("ID").toString();
            sm.deletebyid(id);
        }
    }

    @Transactional
    public void updatetfandlab(HashMap hashMap) {
        //先更新
          sm.updatesub(hashMap);
        //判断表类是否有标签
        String id = lm.selectCountByKey(hashMap.get("ID").toString());
        if (null!=id&& !"0".equals(id)){
            //有就更新
            lm.updateLabel(hashMap);
        }else {
            //没有就插入
            lm.insertlabel(hashMap);
        }



    }

    public HashMap findANS(HashMap hashMap, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, String>> muls = am.getAllANS(hashMap);
        PageInfo<Map<String, String>> pageInfo = new PageInfo<Map<String, String>>(muls);
        HashMap returnMap = new HashMap();
        returnMap.put("total", pageInfo.getTotal());
        returnMap.put("rows", pageInfo.getList());
        return returnMap;
    }

    public void delANS(JSONArray array) {
        for (int i = 0; i <array.size() ; i++) {
            String id = ((Map)array.get(i)).get("ID").toString();
            am.deletebyid(id);
        }
    }

    public void updatesubandlab(HashMap hashMap) {

    //先更新
          am.updateans(hashMap);
    //判断表类是否有标签
    String id = lm.selectCountByKey(hashMap.get("ID").toString());
        if (null!=id&& !"0".equals(id)){
        //有就更新
        lm.updateLabel(hashMap);
    }else {
        //没有就插入
        lm.insertlabel(hashMap);
    }
    }

    public void updatepwd(HashMap hashMap) throws Exception {
        um.updatepwd(hashMap);
    }
}
